var routerURL = "json/route.json";
var menuURL = "json/index_1.json";
 // var routerURL = "http://www.szsantiago.com/webapp/getRoute";
 // var menuURL = "http://www.szsantiago.com/webapp/getMenu";


angular.module('Routing', ['ui.router'])
  .provider('router', function ($stateProvider) {
    var urlCollection;
    this.$get = function ($http, $state) {
      return {
        setUpRoutes: function () {
          $http.get(urlCollection).success(function (collection) {
            for (var routeName in collection) {
              if (!$state.get(routeName)) {
                $stateProvider.state(routeName, collection[routeName]);
              }
            }
          });
        }
      }
    };
    this.setCollectionUrl = function (url) {
      urlCollection = url;
    }
});
var app = angular.module('Santiago', ['ui.router', 'Routing']);
app.config(function ($stateProvider, $urlRouterProvider, routerProvider) {
    $stateProvider
      .state('home', {
        url: '/home',
        templateUrl: 'subpage/home/index.html'
      });
    $urlRouterProvider.otherwise('/home');
    routerProvider.setCollectionUrl(routerURL+'?language=0');
  })
  .run(function (router) {
    router.setUpRoutes();
  });
app.controller('routerCtrl', function($scope, $rootScope ,$state, $http) {
  var url = menuURL;
  $scope.navList;
  $rootScope.routes;
  $http.get(url)
    .success(function(res){
      var items = res.menu;
      var navList = [];
      var routes = [];
      for (var i = 0; i < items.length; i++) {
        if (items[i].menus.length == 0) {
          var tempItem = {
            "name": items[i].englishName,
            "level": 0,
            "url": items[i].menuChain,
            "order": items[i].menuOrder,
            "child": ""
          };
          var route = {
            "state":items[i].menuChain,
            "url":'/'+items[i].menuChain,
            "templateUrl":'subpage/'+items[i].menuChain+'/index.html'
          };
          navList.push(tempItem);
          routes.push(route);
        }
        else {
          var tempChildList = new Array();
          for (var x = 0; x < items[i].menus.length; x++) {
            var tempChildItem = {
              "name": items[i].menus[x].englishName,
              "level": 1,
              "url": items[i].menus[x].menuChain,
              "order": items[i].menus[x].menuOrder,
              "img": "/"+items[i].menus[x].menuPicture
            };
            var route = {
              "state":items[i].menus[x].menuChain,
              "url":'/'+items[i].menus[x].menuChain,
              "templateUrl":'subpage/'+items[i].menus[x].menuChain+'/index.html'
            };
            routes.push(route);
            tempChildList.push(tempChildItem);
          }
          tempChildList.sort(function(a,b){return a.order>b.order?1:-1});
          var tempItem = {
            "name": items[i].englishName,
            "level": 1,
            "url": items[i].menuChain,
            "order": items[i].menuOrder,
            "child": tempChildList
          };
          navList.push(tempItem);
        }
      }
      navList.sort(function(a,b){return a.order>b.order?1:-1});
      $scope.navList = navList;
      $rootScope.routes = routes;
    })
    .error(function() {
      $scope.navList = [
        {
          "name": "Home",
          "level": 0,
          "url": 'home',
          "order": 1,
          "child": ""
        },
        {
          "name": "About Us",
          "level": 0,
          "url": 'aboutUs',
          "order": 2,
          "child": ""
        },
        {
          "name": "Recruitment",
          "level": 0,
          "url": 'recruitment',
          "order": 5,
          "child": ""
        },
        {
          "name": "Q Plant Series",
          "level": 1,
          "order":3,
          "child": [
            {
              "name": "Product Philosophy",
              "url": "productPhilosophy",
              "order":1
            },
            {
              "name": "Q Plant1",
              "url": "QPlant1",
              "order":2
            },
            {
              "name": "Q Plant1S",
              "url": "QPlant1S",
              "order":3
            },
            {
              "name": "Q Plant2",
              "url": "QPlant2",
              "order":4
            }
          ]
        },
        {
          "name": "iOS Dual Sim Series",
          "level": 1,
          "order":4,
          "child": [
            {
              "name": "Dual Sim Power Case",
              "url": "DualSimPowerCase",
              "order":1
            },
            {
              "name": "DUO SIM",
              "url": "DUOSIM",
              "order":2
            }
          ]
        }
      ];
  });
  $scope.showChildArr = [];
  $scope.showChild = function(level,index){
    if (level==1) {
      $scope.showChildArr = [];
      $scope.showChildArr[index] = true;
    }else {
      $scope.showChildArr = [];
      // if (document.body.clientWidth < 1024) {
      //   $(".nav-item-first-ul").css("display","none");
      //   $scope.menuIcon = true;
      // }
    }
  }
  $scope.showChildSpan = function(level,index){
    if (document.body.clientWidth < 1024) {
      $(".nav-item-first-ul").css("display","none");
      $scope.menuIcon = true;
    }
  }
  $scope.dispearChildUl = function(){
    $scope.showChildArr = [];
  }
  $scope.menuIcon = true;
  $scope.showIcon = function(flag){
    $scope.menuIcon = !$scope.menuIcon;
    if(flag==1) {
      $(".nav-item-first-ul").css("display","block");
    }else {
      $(".nav-item-first-ul").css("display","none");
    }
  }
  $scope.showChildli = function(){
    if (document.body.clientWidth < 1024) {
      $(".nav-item-first-ul").css("display","none");
      $scope.menuIcon = true;
    }else {
      $scope.showChildArr = [];
    }
  }
});
$(window).resize(function() {
  if (document.body.clientWidth > 1024) {
    $(".nav-item-first-ul").css("display","block");
  }
});
